import * as module from "@/tools/util";
import * as Request from '@/api/liubike';
import store from "@/tools/factory"
import { config, isDev } from '@/api/config';

import wx from '@/tools/wx'
import commonJs from '@/views/commonJs/common'
let that
let loginInfo = null;

/**
 * 转化:   that.data -> that._data
 * ev.detail -> ev.target
 * options -> this.$route.query
 */

export default {
    mixins: [commonJs],
    data() {
        return {
            title: "加载中...", // 状态
            list: [], // 数据列表
            cardList: [],
            type: "", // 数据类型
            isActive: null,
            loading: true, // 显示等待框
            disabled: false
        }
    },
    create() {

    },
    mounted() {

        that = this;
        if (this.$route.query && this.$route.query.type == 2) {
            that.setData({
                isActive: 2
            })
        } else {
            that.setData({
                isActive: 1
            })
        }


        if (this._data.isActive == 1) {
            that.setData({
                list: []
            })
            this.getInfo();
        } else {
            that.setData({
                list: []
            })
            this.getPackage()
        }
    },
    methods: {
        onShareAppMessage: function () {
            return {
                title: '小遛共享',
                path: '/index',
            }
        },
        onLoad(options) {
            that = this;
            if (options && options.type == 2) {
                that.setData({
                    isActive: 2
                })
            } else {
                that.setData({
                    isActive: 1
                })
            }
        },
        onShow() {
            if (this._data.isActive == 1) {
                that.setData({
                    list: []
                })
                this.getInfo();
            } else {
                that.setData({
                    list: []
                })
                this.getPackage()
            }
        },
        tabChange(e) {
            let index = e.currentTarget.dataset.index;
            if (that._data.isActive == index) {
                return false
            }
            that.setData({
                isActive: index
            })
            if (index == 1) {
                this.getInfo()
            } else {
                this.getPackage()
            }
        },
        getPackage() {
            wx.showLoading({
                title: '加载中...',
            })
            Request.getPackageList().then(res => {
                wx.hideLoading();
                if (!res.success) {
                    wx.showToast({
                        title: req.msg,
                        icon: 'none',
                        duration: 2000
                    })
                    return false
                }
                that.setData({
                    cardList: res.data
                })
            }).catch(err => {
                wx.hideLoading();
            })
        },
        getInfo() { // 获取优惠券列表
            wx.showLoading({
                title: '加载中...',
            })
            Request.getCouponList({
                offset: 0,
                limit: 50
            }).then(res => {
                if (res.length == 0) {
                    wx.hideLoading();
                    return;
                }
                res.forEach((item, index) => {
                    if (that.compare(item.endtime)) {
                        item.over = true;
                    } else {
                        item.over = false;
                    }
                    if (item.coupontype == 0) {
                        item.money = item.money.toFixed(2)
                    }
                    item.payMethodLimit = item.payMethodLimit.replace(/,/g, "");
                    let date = new Date(parseInt(item.endtime));
                    item.valid = that.fomatDate(date, "yyyy.MM.dd");
                });
                that.setData({
                    list: res
                })
                wx.hideLoading();
            }).catch(err => {
                wx.hideLoading();
                wx.showToast({
                    title: '小遛需要缓缓',
                    icon: 'none',
                    duration: 2000
                })
            });
        },
        compare(time) {
            let status = new Date().getTime() > parseInt(time) ? true : false;
            return status;
        },
        exchange() {
            wx.navigateTo({
                url: `/card`
            });
        },
        fomatDate(date, fmt) {
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
            }
            let o = {
                'M+': date.getMonth() + 1,
                'd+': date.getDate(),
                'h+': date.getHours(),
                'm+': date.getMinutes(),
                's+': date.getSeconds()
            };
            for (let k in o) {
                if (new RegExp(`(${k})`).test(fmt)) {
                    let str = o[k] + '';
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : that.padLeftZero(str));
                }
            }
            return fmt;
        },
        padLeftZero(str) {
            return ('00' + str).substr(str.length);
        }
    }
}
